<?php

class Member{
	protected $_id;
	protected $_name;
	protected $_username;
	protected $_access_level;
	protected $_manager_id;
	protected $_type_id;
	protected $_type_name;

	// constructor
	function __construct($obj){
		$this->_id = $obj->id;
		$this->_name =  $obj->name;
		$this->_username = $obj->username;
		$this->_manager_id = $obj->managed_by;
		$this->_type_id = $obj->type_id;

		// identify the access level
		$userTypes = new UserTypes();
		$row = $userTypes->fetchRow("id=" . $this->_type_id);
		$this->_access_level = $row->access_level;
		$this->_type_name = $row->type_name;
	}

	function getName(){
		return $this->_name;
	}

	function getAccessLevel(){
		return $this->_access_level;
	}

	function getType(){
		return $this->_type_name;
	}

	function getId(){
		return $this->_id;
	}

	function authorize($controller, $action){
		$registry = Zend_Registry::getInstance();
		$access_levels = $registry->get('access_level_config');
		switch($this->_access_level){
			case $access_levels->admin_level:
				$retValue = true;
				break;
			case $access_levels->salesman_level:
				if($controller == 'salesman' || $controller == "index" || $controller == "error" )
					$retValue = true;
				else{
					if($controller == 'member'){
						if($action == 'listagent' || $action == 'editagent' || $action == 'searchagent'){
							return true;
						}
					}
					$retValue = false;
				}
				break;
			case $access_levels->others_level:
				if($controller == 'agent' || $controller == "index" || $controller == "error")
					$retValue = true;
				else
					$retValue = false;
				break;
			default:
				$retValue = false;
				break;
		}
		return $retValue;
	}

	function getDefaultValues(){
		$registry = Zend_Registry::getInstance();
		$access_levels = $registry->get('access_level_config');
		$level_name = 'level' . $this->_access_level;
		$retValue = array();
		$retValue["controller"] = $access_levels->$level_name->default_controller;
		$retValue["action"] = $access_levels->$level_name->default_action;
		return $retValue;
	}


}
?>